#include "pWidget.h"

#include "notify_adaptor.h"
#include "notify_interface.h"

#include <QDir>
#include <fstream>
//#include <iostream>

using namespace std;

QDigitalClock::QDigitalClock(QWidget* parent): QWidget(parent) {
    sync = new QTimer();
    clock = new QTime(QTime::currentTime ());
    connect(sync, SIGNAL(timeout()), this, SLOT(update()));
    sync->start(1000);

    tFont.setPointSize(14);

    QPainter sizers(this);

    sizers.setFont(tFont);

    setGeometry(x(),y(), sizers.fontMetrics().width("99:99") + 15 ,25);
}

QDigitalClock::~QDigitalClock(){
    delete sync;
}

void QDigitalClock::paintEvent(QPaintEvent * event) {
    delete clock;
    clock = new QTime(QTime::currentTime ());
    QPainter p(this);

    p.setRenderHint(QPainter::TextAntialiasing);
    p.setRenderHint(QPainter::Antialiasing);
    tFont.setPointSize(14);

    p.setPen(Qt::white);
    p.setBrush(Qt::white);

    QString time;
    int h,m;
    h = clock->hour();
    m= clock->minute();
    if (m>9) time = QString::number(h) + ":" +QString::number(m);
    else time = QString::number(h) + ":0" +QString::number(m);

    p.setFont(tFont);

    p.save();
    //this->setGeometry(x(),y(),p.fontMetrics().width(time), 25);
    p.drawText(0,2,p.fontMetrics().width(time),p.fontMetrics().height(),Qt::AlignCenter, time);
    p.restore();

    p.end();
}

QBattery::QBattery(QWidget * parent ) : QWidget(parent){
    charge =0;
    Charging=false;
    setGeometry(x(),y(), 16, height());

    new NotifyAdaptor(this);
    QDBusConnection::sessionBus().registerObject("/", this);
    com::opendesk::notify *iface;
    iface = new com::opendesk::notify(QString(), QString(),QDBusConnection::sessionBus(),this);

    connect(iface, SIGNAL(battery(bool,int)), this, SLOT(setLevel(bool,int)));

    animationTime = 0;
    chargeAnimation = new QTimer();

}

void QBattery::setLevel(bool c,int l){
    charge= l;
    Charging=c;
    if (c && ! chargeAnimation->isActive()) {
        chargeAnimation->start(1000);
        connect(chargeAnimation, SIGNAL(timeout()), this, SLOT(animationUpdate()));
    }
    if (!c && chargeAnimation->isActive()) {
        chargeAnimation->stop();
    }
    update();
}

QBattery::~QBattery(){}

void QBattery::paintEvent(QPaintEvent * event){
    QPainter p(this);

    p.setRenderHint(QPainter::TextAntialiasing);
    p.setRenderHint(QPainter::Antialiasing);

    /*p.save();
    p.drawImage(0,2,QImage("Icons/battery-70.png"));
    p.restore();*/

    p.setPen(Qt::darkGray);
    p.setBrush(Qt::darkGray);
    p.save();
    p.drawRect(5,3,4,3);
    p.drawRect(2,6,10,16);
    p.restore();

    if (!Charging){

        if (charge >= 75) {
            p.setPen(Qt::white);
            p.setBrush(Qt::white);
            p.save();
            p.drawRect(5,3,4,3);
            p.drawRect(2,6,10,16);
            p.restore();
        }

        if (charge >= 50 && charge < 75) {
            p.setPen(Qt::white);
            p.setBrush(Qt::white);
            p.save();
            p.drawRect(2,10,10,12);
            p.restore();

        }

        if (charge >= 25 && charge < 50) {
            p.setPen(Qt::white);
            p.setBrush(Qt::white);
            p.save();
            p.drawRect(2,14,10,8);
            p.restore();

        }

        if (charge >= 10 && charge < 25) {
            p.setPen(Qt::white);
            p.setBrush(Qt::white);
            p.save();
            p.drawRect(2,18,10,4);
            p.restore();

        }

        if (charge >= 0 && charge < 10) {
            p.setPen(Qt::red);
            p.setBrush(Qt::red);
            p.save();
            p.drawRect(2,21,10,1);
            p.restore();

        }
    }
    else {
        p.setPen(Qt::darkGray);
        p.setBrush(Qt::darkGray);
        p.save();
        p.drawRect(5,3,4,3);
        p.drawRect(2,6,10,16);
        p.restore();
    //    std::cout<<animationTime<<"\n";!charging &&
        int t = animationTime;
        if (t==4) {
            p.setPen(Qt::white);
            p.setBrush(Qt::white);
            p.save();
            p.drawRect(5,3,4,3);
            p.drawRect(2,6,10,16);
            p.restore();
        }

        if (t == 3) {
            p.setPen(Qt::white);
            p.setBrush(Qt::white);
            p.save();
            p.drawRect(2,10,10,12);
            p.restore();

        }

        if (t == 2) {
            p.setPen(Qt::white);
            p.setBrush(Qt::white);
            p.save();
            p.drawRect(2,14,10,8);
            p.restore();

        }

        if (t==1) {
            p.setPen(Qt::white);
            p.setBrush(Qt::white);
            p.save();
            p.drawRect(2,18,10,4);
            p.restore();

        }

        if ( t == 0) {
            p.setPen(Qt::white);
            p.setBrush(Qt::white);
            p.save();
            p.drawRect(2,21,10,1);
            p.restore();

        }
    }
    p.end();
}

void QBattery::animationUpdate(){
    if (animationTime < 4) {
        animationTime++;
        update();
    }
    else {
        animationTime = 0;
        update();
    }
}

QString readPath(){
    QString p, ret;
    string stream;
    p=QDir::homePath()+"/.opendesk/config/look";
    qWarning(p.toStdString().c_str());
    ifstream fin(p.toStdString().c_str());
    while (fin >> stream) {
        if (stream[0]=='#') {
            qWarning(stream.c_str());
        }
        if (stream=="ICONS") {
            fin >> stream;
            p = QString::fromStdString(stream);
            return p;
        }
    }
    p="/usr/pixmaps/opendesk/icons/";
    return p;
}

QNotification::QNotification(QWidget * parent) : QWidget(parent) {
    setGeometry(2,y(),400,90);

    alert=true;

    new NotifyAdaptor(this);
    QDBusConnection::sessionBus().registerObject("/", this);
    com::opendesk::notify *iface;
    iface = new com::opendesk::notify(QString(), QString(),QDBusConnection::sessionBus(),this);

    connect(iface, SIGNAL(message(QString,QString)), this, SLOT(newMessage(QString,QString)));
    connect(iface, SIGNAL(call(QString)), this, SLOT(newCall(QString)));
    connect(iface, SIGNAL(battery(bool,int)), this, SLOT(batteryAllert(bool,int)));

    path = readPath();
    qWarning(path.toStdString().c_str());
}

QNotification::~QNotification(){
    messages.clear();
    calls.clear();
}

void QNotification::paintEvent(QPaintEvent * event) {
    QPainter p(this);
    p.setRenderHint(QPainter::TextAntialiasing);
    p.setRenderHint(QPainter::Antialiasing);
    int nn=0, mn =0;
    QFont mFont;
    mFont.setPointSize(13);
    p.setPen(Qt::white);
    p.setBrush(Qt::white);
    p.setFont(mFont);
    if (messages.size()>0){
        p.save();
        p.drawImage(5,0,QImage(path+"notify/message-notify.png"));
        nn++;
        mn++;
        p.restore();
        p.drawImage(8,70,QImage(path+"notify/message-notify.png"));
        QString text;
        if (messages.size()==1){
            text = "A new message from "+messages.at(0).From+".";
            p.drawText(39,83,text);
        }
        else {
            text = "There are "+QString::number(messages.size())+" new messages.";
            p.drawText(39,83,text);
        }
    }
    if (alert){
        int x;
        p.save();
        x=28*nn;
        p.drawImage(x+5,0,QImage(path+"notify/alert-notify.png"));
        p.restore();
        nn++;
    }
    if (calls.size()>0){
        int x;
        p.save();
        x=23*nn;
        p.drawImage(x+5,0,QImage(path+"notify/call-notify.png"));
        p.restore();
        nn++;
        p.drawImage(5,70-(20*mn),QImage(path+"notify/call-notify.png"));
        QString text;
        if (calls.size()==1){
            text = "A new call from "+calls.at(0)+".";
            p.drawText(39,83-(20*mn),text);
        }
        else {
            text = "There are "+QString::number(calls.size())+" new calls.";
            p.drawText(39,83-(20*mn),text);
        }
        mn++;
    }

    p.end();
}

void QNotification::newMessage(QString From, QString Message){
    message temp;
    temp.Message=Message;
    temp.From=From;
    messages.push_back(temp);
    QString warning = "New message from: "+From+"\nThere are "+QString::number(messages.size())+" unread messages";
    qWarning(warning.toStdString().c_str());
    update();
}

void QNotification::newCall(QString from){
    calls.push_back(from);
    QString warning = "New call from: "+from+"\nThere are "+QString::number(calls.size())+" calls";
    qWarning(warning.toStdString().c_str());
    update();
}

void QNotification::batteryAllert(bool charging, int level){
    if (!charging && level <= 10) {
        alert= true;
    }
    else alert = false;
   // qWarning("Battery message recived!");
    update();
}
